package com.d3works.apps.stock.service.config;

import com.d3works.apps.base.db.Company;
import com.d3works.apps.stock.db.StockConfig;
import com.d3works.apps.stock.db.StockLocation;
import com.d3works.apps.stock.exception.StockExceptionMessage;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;

public class StockConfigService {

  public StockConfig getStockConfig(Company company) throws D3worksException {

    StockConfig stockConfig = company.getStockConfig();

    if (stockConfig == null) {
      throw new D3worksException(
          company,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(StockExceptionMessage.STOCK_CONFIG_1),
          company.getName());
    }

    return stockConfig;
  }

  /** ****************************** STOCK LOCATION ******************************************* */
  public StockLocation getInventoryVirtualStockLocation(StockConfig stockConfig)
      throws D3worksException {

    if (stockConfig.getInventoryVirtualStockLocation() == null) {
      throw new D3worksException(
          stockConfig,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(StockExceptionMessage.STOCK_CONFIG_2),
          stockConfig.getCompany().getName());
    }

    return stockConfig.getInventoryVirtualStockLocation();
  }

  public StockLocation getSupplierVirtualStockLocation(StockConfig stockConfig)
      throws D3worksException {

    if (stockConfig.getSupplierVirtualStockLocation() == null) {
      throw new D3worksException(
          stockConfig,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(StockExceptionMessage.STOCK_CONFIG_3),
          stockConfig.getCompany().getName());
    }

    return stockConfig.getSupplierVirtualStockLocation();
  }

  public StockLocation getCustomerVirtualStockLocation(StockConfig stockConfig)
      throws D3worksException {

    if (stockConfig.getCustomerVirtualStockLocation() == null) {
      throw new D3worksException(
          stockConfig,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(StockExceptionMessage.STOCK_CONFIG_4),
          stockConfig.getCompany().getName());
    }

    return stockConfig.getCustomerVirtualStockLocation();
  }

  public StockLocation getReceiptDefaultStockLocation(StockConfig stockConfig)
      throws D3worksException {

    if (stockConfig.getReceiptDefaultStockLocation() == null) {
      throw new D3worksException(
          stockConfig,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(StockExceptionMessage.STOCK_CONFIG_RECEIPT),
          stockConfig.getCompany().getName());
    }

    return stockConfig.getReceiptDefaultStockLocation();
  }

  public StockLocation getPickupDefaultStockLocation(StockConfig stockConfig)
      throws D3worksException {
    if (stockConfig.getPickupDefaultStockLocation() == null) {
      throw new D3worksException(
          stockConfig,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(StockExceptionMessage.STOCK_CONFIG_PICKUP),
          stockConfig.getCompany().getName());
    }
    return stockConfig.getPickupDefaultStockLocation();
  }
}
